<!DOCTYPE html>
<html>
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>传智健康</title>
        <meta name="description" content="传智健康">
        <meta name="keywords" content="传智健康">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="../plugins/elementui/index.css">
        <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="../css/style.css">
    </head>
    <body class="hold-transition">
        <div id="app">
            <div class="content-header">
                <h1>预约管理<small>预约列表</small></h1>
                <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                    <el-breadcrumb-item>预约管理</el-breadcrumb-item>
                    <el-breadcrumb-item>预约列表</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
            <div class="app-container">
                <div class="box">
                    <div class="filter-container">
                        <el-form id="a" type="text">
                        <el-input placeholder="档案号/姓名/手机号" type="text" onkeyup="vue.findMatchPage()" v-model="pagination.queryString1" style="width: 200px;" class="filter-item"></el-input>
                        <el-button @click="findMatchPage()" class="dalfBut">查询</el-button>
                        <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                        <el-button size="mini" type="danger" @click="handleDeleteAll()">批量取消预约</el-button>
                        </el-form>
                    </div>
                    <el-table size="small" current-row-key="id" :data="dataList" @selection-change="handleSelectionChange" stripe highlight-current-row>
                        <el-table-column type="selection" width="55"></el-table-column>
                        <el-table-column prop="orderDate" label="预约日期" align="center"></el-table-column>
                        <el-table-column prop="code" label="档案号" align="center"></el-table-column>
                        <el-table-column prop="name" label="姓名" align="center"></el-table-column>
                        <el-table-column prop="phoneNumber" label="手机号" align="center"></el-table-column>
                        <el-table-column prop="orderType" label="预约类型" align="center"></el-table-column>
                        <el-table-column prop="orderStatus" label="预约状态" align="center"></el-table-column>
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="send4Success(scope.row)">确认</el-button>
                                <el-button size="mini" type="danger" @click="handleDelete(scope.row)">取消</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="pagination-container">
                        <el-pagination
                            class="pagiantion"
                            @current-change="handleCurrentChange"
                            background=""
                            :current-page="pagination.currentPage"
                            :page-size="pagination.pageSize"
                            layout="total, prev, pager, next, jumper"
                            :total="pagination.total">
                        </el-pagination>
                    </div>
                    <!-- 新增标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="新增预约" :visible.sync="dialogFormVisible">
                            <template>
                                <el-tabs v-model="activeName" type="card">
                                    <el-tab-pane label="基本信息" name="first">
                                        <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                                                 label-width="100px">
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="预约日期" prop="orderDate">
                                                        <el-date-picker
                                                                v-model="formData.orderDate"
                                                                type="date"
                                                                placeholder="选择日期"
                                                                :picker-options="pickerOptions0"
                                                                format="yyyy-MM-dd"
                                                                value-format="yyyy-MM-dd">
                                                        </el-date-picker>
                                                    </el-form-item>

                                                </el-col>
                                                <el-col :span="12">
                                                    <el-form-item label="姓名" prop="name">
                                                        <el-input v-model="formData.name"/>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="手机号" prop="phoneNumber">
                                                        <el-input v-model="formData.phoneNumber"/>
                                                    </el-form-item>
                                                </el-col>
                                                <el-col :span="12">
                                                    <el-form-item label="性别" prop="sex">
                                                        <el-select v-model="formData.sex">
                                                            <el-option label="男" value="1"></el-option>
                                                            <el-option label="女" value="2"></el-option>
                                                        </el-select>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="预约类型" prop="orderType">
                                                        <el-input v-model="formData.orderType" :disabled="true"/>
                                                    </el-form-item>
                                                </el-col>
                                                <el-col :span="12">
                                                    <el-form-item label="预约状态" prop="orderStatus">
                                                        <el-select v-model="formData.orderStatus">
                                                            <el-option label="未到诊" value="未到诊"></el-option>
                                                            <el-option label="已到诊" value="已到诊"></el-option>
                                                        </el-select>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                        </el-form>
                                    </el-tab-pane>
                                    <el-tab-pane label="套餐信息" name="second">
                                        <div class="checkScrol">
                                            <table class="datatable">
                                                <thead>
                                                <tr>
                                                    <th>选择</th>
                                                    <th>项目编码</th>
                                                    <th>项目名称</th>
                                                    <th>项目说明</th>
                                                </tr>
                                                </thead>
                                                <tbody>
                                                <tr v-for="c in tableData">
                                                    <td>
                                                        <el-radio-group :id="c.id" v-model="setmealId"  :value="c.id">
                                                            <el-radio :label="c.id">{{""}}</el-radio>
                                                        </el-radio-group>
                                                    </td>
                                                    <td><label :for="c.id">{{c.code}}</label></td>
                                                    <td><label :for="c.id">{{c.name}}</label></td>
                                                    <td><label :for="c.id">{{c.remark}}</label></td>
                                                </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </el-tab-pane>

                                    <el-tab-pane label="地址信息" name="third">
                                        <div class="checkScrol">
                                            <table class="datatable">
                                                <thead>
                                                <tr>
                                                    <th>选择</th>
                                                    <th>预约地址</th>
                                                </tr>
                                                </thead>
                                                <tbody>
                                                <tr v-for="c in tableData2">
                                                    <td>
                                                        <el-radio-group :id="c.id" v-model="addressId"  :value="c.id">
                                                            <el-radio :label="c.id">{{""}}</el-radio>
                                                        </el-radio-group>
                                                    </td>
                                                    <td><label :for="c.id">{{c.name}}</label></td>
                                                </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </el-tab-pane>
                                </el-tabs>
                            </template>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible = false">取消</el-button>
                                <el-button type="primary" @click="handleAdd()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>
                </div>
            </div>
        </div>
    </body>
    <!-- 引入组件库 -->
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <script>
        var vue = new Vue({
            el: '#app',
            data:{
                pagination: {//分页相关模型数据
					  currentPage: 1,//当前页码
					  pageSize:10,//每页显示的记录数
					  total:0,//总记录数
					  queryString1:""//查询条件
				},
                multipleSelection:[],//复选框选中数据
				dataList: [],//当前页要展示的分页列表数据
                formData: {},//表单数据
                tableData:[],
                tableData2:[],
                setmealId:"",//套餐的id
                addressId:"",
                dialogFormVisible: false,//增加表单是否可见
                dialogFormVisible4Edit:false,//编辑表单是否可见
                pickerOptions0: {  // 只能选择今天以及今天之后的日期
                    disabledDate(time) {
                        return time.getTime() < Date.now();
                    }
                },
                rules: {//校验规则
                    orderDate: [{required: true, message: '日期为必填项', trigger: 'blur'}],
                    name: [{required: true, message: '姓名为必填项', trigger: 'blur'}],
                    phoneNumber: [{required: true, message: '手机号为必填项', trigger: 'blur'},
                        {pattern: /^1[34578]\d{9}$/, message: '请输入正确的手机号'}]
                }
            },
            //钩子函数，VUE对象初始化完成后自动执行
            created() {
                //VUE对象初始化完成后调用分页查询方法,完成分页查询
                this.findPage();
            },
            methods: {

                //添加
                handleAdd () {
                    //进行表单校验
                    this.$refs['dataAddForm'].validate((valid)=> {
                        if( this.setmealId != "" && this.addressId != ""){
                            if (valid){  //校验通过发送ajax请求,将数据提交到后台
                                console.log(this.formData);
                                axios.post("/ordersettinglist/add.do?setmealId="+this.setmealId,this.formData,this.addressId).then((res)=>{
                                    //关闭添加的窗口(不管执行成功还是失败)
                                    this.dialogFormVisible = false;
                                    if(res.data.flag) {
                                        //添加成功重新调用分页查询
                                        this.findPage();
                                        //然后弹出提示信息
                                        this.$message({
                                            message:res.data.message,
                                            type:'success'
                                        });
                                    } else {//失败的
                                         //弹出提示信息
                                        this.$message.error(res.data.message);
                                        }
                                });
                            }else {  //校验不通过
                                this.$message.error("输入有误,")
                                return false;
                            }
                        }else {
                            this.$message.error("请选择套餐和地址");
                        }
                    });
                },
                findMatchPage(){
                    this.pagination.currentPage = 1;
                    this.findPage();
                },
                //分页查询
                findPage() {
                    var param = {
                        currentPage: this.pagination.currentPage,
                        pageSize: this.pagination.pageSize,
                        queryString: this.pagination.queryString1
                    };
                    axios.post("/ordersettinglist/findPage.do",param).then((res) => {
                        //解析Controller相应的数据
                        this.pagination.total = res.data.total;
                        this.dataList = res.data.rows;
                    });
                },
                // 重置表单
                resetForm() {
                    this.formData = {
                        orderType :"电话预约",
                        orderStatus :"未到诊"
                    };  //重置数据
                },
                // 弹出添加窗口
                handleCreate() {
                    this.resetForm();
                    this.dialogFormVisible = true;
                    this.activeName = 'first';
                    this.setmealId = "";
                    this.addressId = "";
                    //查询所有套餐信息,赋值给tableData
                    axios.get("/setmeal/findAll.do").then((res)=>{
                        if (res.data.flag){
                            this.tableData = res.data.data;
                        }else {
                            this.$message.error(res.data.message);
                        }
                    });
                    //查询所地址信息,赋值给tableData2
                    axios.get("/address/findAllll.do").then((res)=>{
                        if (res.data.flag){
                            this.tableData2 = res.data.data;
                        }else {
                            this.$message.error(res.data.message);
                        }
                    });
                },
                // 发送短信确认预约
                send4Success(row) {
                    //发送ajax请求 ,发送短消息
                    axios.post("/ordersettinglist/send4Success.do?telephone=" + row.phoneNumber + "&orderDate=" + row.orderDate).then((res) => {
                        if (res.data.flag) {
                            //短信发送成功
                            this.$message.success(res.data.message);
                        }else{
                            //短信发送失败
                            this.$message.error(res.data.message);
                        }
                    });
                },
                //切换页码
                handleCurrentChange(currentPage) {
                    this.pagination.currentPage = currentPage;
                    //重新调用findPage方法
                    this.findPage();
                },

                showMessage(res){
                    if (res == 'Error: Request failed with status code 403') {
                        //权限不足
                        this.$message.error('无访问权限');
                        return;
                    }else {
                        this.$message.error('未知错误');
                        return;
                    }
                },
                //批量删除
                handleDeleteAll(){
                    this.$confirm("确定删除当前数据?","提示",{
                        type:'warning'
                    }).then(()=>{
                        axios.post("/ordersettinglist/deleteAll.do",this.multipleSelection).then((res)=>{
                            this.findPage();
                            if(res.data.flag){
                                this.$message.success(res.data.message)
                            }else {
                                this.$message.error(res.data.message)
                            }
                        }).catch((res)=>{
                            this.showMessage(res);
                        });
                    }).catch(()=>{
                        //alert('取消')
                        this.$message({
                            type:'success',
                            message:'已取消操作'
                        });
                    });
                },
                //将选中的复选框的值拿出来
                handleSelectionChange(val) {
                    this.multipleSelection = val;
                },
                // 删除
                handleDelete(row) {
                    this.$confirm("确定删除当前数据?","提示",{
                        type:'warning'
                    }).then(()=>{
                        //发送ajax请求,将ID提交
                        axios.post("/ordersettinglist/delete.do?id="+row.id).then((res)=>{
                            if (res.data.flag){
                                //成功就提示信息
                                this.$message.success(res.data.message);
                                this.findPage();  //重新分页查询
                                //发送ajax请求 ,发送短消息
                                axios.post("/ordersettinglist/send4Cancel.do?telephone=" + row.phoneNumber).then((res) => {
                                    if (res.data.flag) {
                                        //短信发送成功
                                        this.$message.success(res.data.message);
                                    }else{
                                        //短信发送失败
                                        this.$message.error(res.data.message);
                                    }
                                });
                            }else{
                                //执行失败
                                this.$message.error(res.data.message);
                            }
                        }).catch((res)=>{
                            this.showMessage(res);
                        });
                    }).catch(()=>{
                        //alert('取消')
                        this.$message({
                            type:'success',
                            message:'已取消操作'
                        });
                    });
                }
            }
        })
    </script>
</html>